package com.hxd.mall.utils.local

import android.annotation.SuppressLint
import android.app.ActivityManager
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.key_web_url
import com.hxd.mall.constant.HxdConstantsRouterPath.Common.path_to_webview
import com.hxd.mall.constant.HxdGlobalInfo.instance
import com.hxd.mall.module.product.home.bean.Resource
import com.hxd.mall.module.school.bean.ItemResource
import com.hxd.mall.module.school.bean.ModuleLayout
import com.hxd.mall.utils.local.NavigationUtil.routerNavigation
import com.hxd.mall.utils.open.GsonUtil
import com.hxd.mall.utils.open.ToastUtil

/**
 * Create by yinzhengwei on 2019-11-04
 * @Function
 */

fun getModule(strList: String?): ModuleLayout? {
    if (strList.isNullOrEmpty()) {
        return null
    }
    return GsonUtil.json2bean(strList, ModuleLayout::class.java)
}

fun getNewListAdapter(list: List<Resource>?, adapter: BaseQuickAdapter<Resource, BaseViewHolder>, point: String? = "") =
    adapter.apply {
        setNewData(list ?: listOf())
        setOnItemClickListener { _, _, position ->
            list?.run {
                this[position].entryUri?.run {
                    routerNavigation(path_to_webview, key_web_url, this)
                    when (point) {
                        "icon_number_click" -> instance.sendPoint(icon_number_click(position + 1))
                        "niche_click" -> instance.sendPoint(niche_click(position + 1))
                        "commercial_college_article_niche_click" -> instance.sendPoint(commercial_college_article_niche_click(position + 1))
                        "commercial_college_music_niche_click" -> instance.sendPoint(commercial_college_music_niche_click(position + 1))
                        "commercial_college_video_niche_click" -> instance.sendPoint(commercial_college_video_niche_click(position + 1))
                    }
                }
            }
        }
    }


fun getListAdapter(list: List<ItemResource>?, adapter: BaseQuickAdapter<ItemResource, BaseViewHolder>, point: String? = "") =
    adapter.apply {
        setNewData(list ?: listOf())
        setOnItemClickListener { _, _, position ->
            list?.run {
                this[position].entryUri?.run {
                    routerNavigation(path_to_webview, key_web_url, this)
                    when (point) {
                        "icon_number_click" -> instance.sendPoint(icon_number_click(position + 1))
                        "niche_click" -> instance.sendPoint(niche_click(position + 1))
                        "commercial_college_article_niche_click" -> instance.sendPoint(commercial_college_article_niche_click(position + 1))
                        "commercial_college_music_niche_click" -> instance.sendPoint(commercial_college_music_niche_click(position + 1))
                        "commercial_college_video_niche_click" -> instance.sendPoint(commercial_college_video_niche_click(position + 1))
                    }
                }
            }
        }
    }

fun setPrimaryClip(context: Context, text: String) {
    (context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager).run {
        setPrimaryClip(ClipData.newPlainText(null, text))

        if (text.isNotEmpty())
            ToastUtil.showToast("复制成功")
    }
}

fun getPrimary(context: Context) =
    (context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager).primaryClip?.getItemAt(0)?.text.toString().trim()

/**
 * 获得栈中最顶层的Activity
 *
 * @param context
 * @return
 */
@SuppressLint("NewApi")
fun getTopActivity(context: Context): String {
    val manager = context.getSystemService(Context.ACTIVITY_SERVICE)as ActivityManager
    val runningTaskInfos = manager.getRunningTasks(1);

    return if (runningTaskInfos != null) {
        runningTaskInfos.get(0).topActivity?.getClassName()?:""
    } else
        ""
}

